<?php

namespace App\Console\Commands;

use App\Admin\Models\SiteCompany;
use App\Store\Models\Member\Member;
use App\Store\Models\Message\Rule;
use App\Store\Services\Message\Lianlu;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * 生日回访
 */
class GetBirthdayMember extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:GetBirthdayMemberList';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取生日用户列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * @return true
     */
    public function handle()
    {
        Log::info('获取生日用户列表开始');
        $this->getmemberlist();
        Log::info('获取生日用户列表结束');

        return true;
    }


    /**
     * @return true
     */
    public function getmemberlist()
    {
        $list = Rule::where(['type' => 4, 'status' => 2, 'is_del' => 1])->get()->toArray();
        if (!empty($list)) {
            foreach ($list as $rule) {
                $this->getBirthday($rule);
            }
        } else {
            Log::info('没有配置生日规则');
        }

        return true;
    }


    /**
     * @param $rule
     * @return bool|void
     */
    public function getBirthday($rule)
    {
        $m_d = date('m-d');



        $list = Member::query()
            ->where(['cp_id' => $rule['cp_id'], 'store_id' => $rule['store_id'],'is_del'=>1])
            ->where('solar_bdate', 'like', '%' . $m_d)
            ->get()
            ->toArray();

        Log::info('获取生日列表',$list);
        $phone_arr = [];
        if (!empty($list)) {
            foreach ($list as $item) {
                if ($item['mobile']) {
                    $phone_arr[] = $item['mobile'];
                }
            }
        }


        try {
            if (!empty($phone_arr)) {

                $companyinfo = SiteCompany::query()->where([['id', $rule['cp_id']], ['is_open_sms', 2]])->first();
                if (empty($companyinfo)) {
                    throw new  \Exception('短信未开启');
                }
                Log::info('公司信息：' . json_encode($companyinfo));
                Log::info('手机号：' . json_encode($phone_arr));
                Log::info('短信内容：' . $rule['content']);

                $res = $this->sendMessage($phone_arr, $rule['content'], $companyinfo);
                if (!empty($res) && $res['status'] == '00') {
                    return true;
                } else {
                    throw new  \Exception('生日短信发送失败' . $res['message'] ?? '');
                }

            }
        } catch (\Exception $e) {
            Log::info('生日短信发送结果：' . $e->getMessage());
            return false;
        }

    }

    public function sendMessage($phone_arr, $content, $companyinfo)
    {
        $res = Lianlu::NormalSend($phone_arr, $content, $companyinfo);
        info('生日短信发送结果' . $res);
        return (array)json_decode($res);

    }
}
